home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / MPW IIGS SC / SC.001.Shell / C Shell / Shell.c next >
Encoding:
C/C++ Source or Header  |  1990-06-24  |  13.6 KB  |  532 lines  |  [TEXT/MPS ]

  1. /*
  2. *    Standard Application Shell  - C Version
  3. *           Developer Technical Support
  4. *
  5. *        v3.0     Mensch, KAAR, Luther
  6. */
  7.  
  8. /*
  9. *     Copyright (c) Apple Computer, Inc. 1988-1990
  10. *                All Rights Reserved.
  11. *
  12. *    Developer Technical Support Apple II Sample Code
  13. *
  14. *    ------------------------------------------------------
  15. *
  16. *    This program and its derivatives are licensed only for
  17. *    use on Apple computers.
  18. *
  19. *    Works based on this program must contain and
  20. *    conspicuously display this notice.
  21. *
  22. *    This software is provided for your evaluation and to
  23. *    assist you in developing software for the Apple IIGS
  24. *    computer.
  25. *
  26. *    DISCLAIMER OF WARRANTY
  27. *
  28. *    THE SOFTWARE IS PROVIDED "AS IS" WITHOUT
  29. *    WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  30. *    WITH RESPECT TO ITS MERCHANTABILITY OR ITS FITNESS
  31. *    FOR ANY PARTICULAR PURPOSE.  THE ENTIRE RISK AS TO
  32. *    THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
  33. *    YOU.  SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU (AND
  34. *    NOT APPLE OR AN APPLE AUTHORIZED REPRESENTATIVE)
  35. *    ASSUME THE ENTIRE COST OF ALL NECESSARY SERVICING,
  36. *    REPAIR OR CORRECTION.
  37. *
  38. *    Apple does not warrant that the functions
  39. *    contained in the Software will meet your requirements
  40. *    or that the operation of the Software will be
  41. *    uninterrupted or error free or that defects in the
  42. *    Software will be corrected.
  43. *
  44. *    SOME STATES DO NOT ALLOW THE EXCLUSION
  45. *    OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY
  46. *    NOT APPLY TO YOU.  THIS WARRANTY GIVES YOU SPECIFIC
  47. *    LEGAL RIGHTS AND YOU MAY ALSO HAVE OTHER RIGHTS
  48. *    WHICH VARY FROM STATE TO STATE.
  49. */
  50.  
  51. #include <types.h>     
  52. #include <gsos.h>                             
  53. #include <locator.h>
  54. #include <adb.h>
  55. #include <intmath.h>
  56. #include <texttool.h>
  57. #include <memory.h>
  58. #include <sane.h>
  59. #include <ace.h>
  60. #include <resources.h>
  61. #include <misctool.h>
  62. #include <scheduler.h>
  63. #include <loader.h>
  64. #include <quickdraw.h>                         
  65. #include <qdaux.h>
  66. #include <event.h>
  67. #include <control.h>
  68. #include <window.h>
  69. #include <menu.h>
  70. #include <lineedit.h>
  71. #include <dialog.h>
  72. #include <sound.h>
  73. #include <notesyn.h>
  74. #include <noteseq.h>
  75. #include <midi.h>
  76. #include <Stdfile.h>
  77. #include <scrap.h>
  78. #include <desk.h>
  79. #include <list.h>
  80. #include <font.h>
  81. #include <print.h>
  82. #include <textedit.h>
  83. #include <video.h>
  84.  
  85. /* part of C libraries */
  86. extern int    _toolErr;
  87.  
  88. /******************************************************************************
  89. *    Application specific defines
  90. */
  91.  
  92. /*    menu item numbers for standard DA menu items */
  93. #define UndoID    250    
  94. #define CutID    251
  95. #define CopyID  252
  96. #define PasteID 253
  97. #define ClearID 254
  98. #define CloseID 255
  99.  
  100. /* application menu item numbers */
  101. #define AboutID 0x1101
  102. #define QuitID  0x1202
  103.  
  104. /* menu numbers */
  105. #define AppleMenuID 0x1100
  106. #define FileMenuID  0x1200
  107. #define EditMenuID  0x1300
  108.  
  109. /* resource ID numbers */
  110. #define BaseResID        0x00000000L    /* start of resource ID numbers */
  111. #define    MenuBarOneRID    0x00001000L    /* resource ID of menu bar */
  112.  
  113. /* Let TaskMaster do it all! */
  114. #define    MyTaskMask        0x001FFFFFL
  115.  
  116.  
  117. /******************************************************************************
  118. *    Standard global variables here
  119. */
  120.  
  121. int        MyMemoryID;                    /* application's memory ID */
  122. boolean Done;                        /* True after Quit menu item is selected */
  123. long    ToolRecRef;                    /* Ref returned by StartUpTools */
  124. int        WindowKind;                    /* active window's kind */
  125.  
  126.  
  127. /*      The following is the record that is used by TaskMaster to return
  128.       events. It is similar to a regular event record, except that there are
  129.       additional fields at the end. The first additional field is used to
  130.       convey some TaskMaster specific data back to the application. The second
  131.       additional field is called the TaskMask and is used to tell TaskMaster
  132.       what situations to handle.  In this shell, we tell TaskMaster to handle
  133.       everything by setting all currently defined bits to 1 (MyTaskMask). */
  134.  
  135. WmTaskRec MyEvent = {
  136.     0,
  137.     0L,
  138.     0L,
  139.     0,0,
  140.     0,
  141.     0L,
  142.     MyTaskMask
  143. };
  144.  
  145.  
  146. /******************************************************************************
  147. *
  148. *    doQuit:        Set the Done flag to true. This tells the Event loop to exit.
  149. *
  150. *    Inputs:        NONE
  151. *    Outputs:    Done set to true
  152. *    Calls:        NONE
  153. */
  154.  
  155. doQuit()
  156. {
  157.     Done = true;
  158. }
  159.  
  160.  
  161. /******************************************************************************
  162. *
  163. *    doAbout:    Bring up an Alert Dialog box with our about message in it.
  164. *
  165. *    Inputs:        NONE
  166. *    Outputs:    NONE
  167. *    Calls:        NONE
  168. */
  169.  
  170. doAbout()
  171. {
  172.     /* alertFlag of 4 = reference is a Resource ID */
  173.     AlertWindow(4,NIL,BaseResID+1);    /* toss result returned by this call */
  174. }
  175.  
  176.  
  177. /******************************************************************************
  178. *
  179. * doMenu:        This routine is called when TaskMaster returns a menu
  180. *                event. It takes the menu item that was hit and calls the
  181. *                proper routine, and then unhilites the menu when it is done.
  182. *
  183. *    Inputs:        TaskData holds menu item selected.
  184. *    Outputs:    NONE
  185. *    Calls:        doAbout, doQuit
  186. */
  187.  
  188. doMenu()
  189. {
  190.     int    menuNum;
  191.     int    itemNum;
  192.     
  193.     menuNum = HiWord(MyEvent.wmTaskData);    /* get menu ID */
  194.     itemNum = LoWord(MyEvent.wmTaskData);    /* and item ID from MyEvent */
  195.     
  196.     switch (itemNum) {
  197.         case AboutID:
  198.             doAbout();    /* show About alert */
  199.             break;
  200.         case QuitID:
  201.             doQuit();    /* set Done flag */
  202.             break;
  203.         case UndoID:
  204.         case CutID:
  205.         case CopyID:
  206.         case PasteID:
  207.         case ClearID:
  208.         case CloseID:    /* close taken care of by TaskMaster */
  209.             break;
  210.         default:
  211.             /* alertFlag of 4 = reference is a Resource ID */
  212.             AlertWindow(4,NIL,BaseResID+2);
  213.             break;    /* toss result returned by this call */
  214.     }
  215.  
  216.     /* The routine has been called. Unhilite the menu and return to the */
  217.     /* Main Event Loop. */
  218.     
  219.     HiliteMenu(false,menuNum);
  220. }
  221.  
  222.  
  223. /******************************************************************************
  224. *
  225. *    doSysChange:    Called by testTopWindow when the active window
  226. *                    has changed to or from a system window.
  227. *
  228. *    Inputs:            Bit 15 of WindowKind is 0 if top window is an application
  229. *                    window, 1 if top window is a system window.
  230. *    Outputs:        NONE
  231. *    Calls:            NONE
  232. */
  233.  
  234. doSysChange()
  235. {
  236.     if (WindowKind & 0x8000) {
  237.         EnableMItem(UndoID);
  238.         EnableMItem(CutID);
  239.         EnableMItem(CopyID);
  240.         EnableMItem(PasteID);
  241.         EnableMItem(ClearID);
  242.         EnableMItem(CloseID);
  243.         
  244.         /* if your edit menu has items that are selectable when a NDA is not
  245.            the active window, remove the next two lines. */
  246.         SetMenuFlag(enableMenu,EditMenuID);
  247.         HiliteMenu(false,EditMenuID);
  248.     }
  249.     else {
  250.         DisableMItem(UndoID);
  251.         DisableMItem(CutID);
  252.         DisableMItem(CopyID);
  253.         DisableMItem(PasteID);
  254.         DisableMItem(ClearID);
  255.         DisableMItem(CloseID);
  256.     
  257.         /* if your edit menu has items that are selectable when a NDA is not
  258.            the active window, remove the next two lines. */
  259.         SetMenuFlag(disableMenu,EditMenuID);
  260.         HiliteMenu(false,EditMenuID);
  261.     }
  262. }
  263.  
  264.  
  265. /******************************************************************************
  266. *
  267. *    testTopWindow:    This routine is called on every time through the event
  268. *                    loop.  If the type to the top window has changed from
  269. *                    application window to system window or back, this routine
  270. *                    will call doSysChange.
  271. *
  272. *    Inputs:            NONE
  273. *    Outputs:        NONE
  274. *    Calls:            doSysChange
  275. */
  276.  
  277. testTopWindow()
  278. {
  279.     WindowPtr        tempWindowPtr;
  280.     unsigned int    tempWindowKind;
  281.     
  282.     tempWindowPtr = FrontWindow();        /* get active window's grafPort */
  283.  
  284.     tempWindowKind = 0;                /* force to application window kind */
  285.     if (tempWindowPtr)                    /* if there is an active window */
  286.         tempWindowKind = GetWKind(tempWindowPtr);    /* get its kind */            
  287.     
  288.     if (tempWindowKind != WindowKind) {
  289.         WindowKind = tempWindowKind;
  290.         doSysChange();
  291.     }
  292. }
  293.  
  294.  
  295. /******************************************************************************
  296. *
  297. *    closeTools:    Shut down the tools I started.
  298. *
  299. *    Inputs:        NONE
  300. *    Outputs:    NONE
  301. *    Calls:        NONE
  302. */
  303.  
  304. closeTools()
  305. {
  306.     /* shut down tools started by StartUpTools */
  307.     ShutDownTools(refIsHandle,ToolRecRef);
  308.     
  309.     /* shut down Memory Manager and Tool Locator */
  310.     MMShutDown(MyMemoryID);
  311.     TLShutDown();
  312. }
  313.  
  314.  
  315. /******************************************************************************
  316. *
  317. *    closeApp:    Close down things. This disposes of all items and
  318. *                memory that we allocated. Usually undoes what was done
  319. *                in initApp.  We don't close our window since _WindShutDown
  320. *                does it for us.
  321. *
  322. *    Inputs:        NONE
  323. *    Outputs:    NONE
  324. *    Calls:        NONE
  325. */                    
  326.  
  327. closeApp()
  328. {
  329.     /* do nothing in this shell */
  330. }
  331.  
  332.  
  333. /******************************************************************************
  334. *
  335. *    eventLoop:    Main Event Loop. Handle things until user selects Quit.
  336. *
  337. *    Inputs:        NONE
  338. *    Outputs:    NONE
  339. *    Calls:        testTopWindow, doMenu
  340. */
  341.  
  342. eventLoop()
  343. {
  344.     word taskCode;
  345.     
  346.     do {
  347.         testTopWindow();        /* test top window to see if it's a NDA */
  348.         
  349.         /* Get event from TaskMaster */
  350.         taskCode = TaskMaster(everyEvent,&MyEvent);
  351.         switch (taskCode) {        /* handle the event for this taskcode */
  352.             /*     With most of these events, we do nothing (in fact, most
  353.                 applications will never see some of these events). You
  354.                 should cut the case labels for events your application does
  355.                 not use out of this switch statement.  Any of these events
  356.                 your application does use should call a function to handle
  357.                 the event and then use a break statement to terminate the
  358.                 rest of the switch statement.
  359.             */
  360.             case nullEvt:
  361.             case mouseDownEvt:
  362.             case mouseUpEvt:
  363.             case keyDownEvt:
  364.             case autoKeyEvt:
  365.             case updateEvt:    
  366.             case activateEvt:
  367.             case switchEvt:
  368.             case deskAccEvt:
  369.             case driverEvt:
  370.             case app1Evt:
  371.             case app2Evt:
  372.             case app3Evt:
  373.             case app4Evt:
  374.             case wInDesk:
  375.                 break;
  376.             case wInMenuBar:    /* do "In system menu bar" events and */
  377.             case wInSpecial:    /* "Item ID selected was 250-255" events */
  378.                 doMenu();
  379.                 break;
  380.             case wClickCalled:
  381.             case wInContent:
  382.             case wInDrag:
  383.             case wInGrow:
  384.             case wInGoAway:
  385.             case wInZoom:
  386.             case wInInfo:
  387.                 break;
  388.             case wInDeskItem:
  389.             case wInFrame:
  390.             case wInactMenu:
  391.             case wClosedNDA:
  392.             case wCalledSysEdit:
  393.             case wTrackZoom:
  394.             case wHitFrame:
  395.             case wInControl:
  396.             case wInControlMenu:
  397.                 break; 
  398.         }
  399.     } while (!Done);            /* Loop until "Quit" is selected */
  400. }
  401.  
  402.  
  403. /******************************************************************************
  404. *
  405. *    initApp:    Perform any application specific initialization. For
  406. *                this app,  we do is initialize the Done to false,
  407. *                set WindowKind to an application window kind, and initialize
  408. *                all of the menus.
  409. *
  410. *                You might use this procedure to create windows,
  411. *                initialize variables and allocate memory needed for
  412. *                the entire program.
  413. *
  414. *    Inputs:        NONE
  415. *    Outputs:    NONE
  416. *    Calls:        NONE
  417. */                    
  418.  
  419. initApp()
  420. {
  421.     Done = false;                    /* we aren't done yet */
  422.     
  423.     WindowKind = 0;                    /* window kind  = application */
  424.  
  425.     /* create default system menu bar from a resource
  426.        and make it the current menu bar */
  427.        
  428.     SetSysBar(NewMenuBar2(refIsResource,MenuBarOneRID,NIL));
  429.     SetMenuBar(NIL);
  430.     
  431.     RefreshDesktop(NIL);            /* redraw the desktop */
  432.     
  433.     InitCursor();                    /* normal arrow cursor */
  434.     
  435.     FixAppleMenu(AppleMenuID);        /* add NDAs to Apple menu */
  436.     FixMenuBar();                    /* set menu bar height and toss result */
  437.     DrawMenuBar();                    /* draw the menu bar */
  438. }
  439.  
  440.  
  441. /******************************************************************************
  442. *
  443. *    errorCheck:    This routine is called by initTools to check for startup
  444. *                errors. An error message is shown and everything is
  445. *                shut down if any errors are detected.
  446. *
  447. *    Inputs:        where = the reference number that tells you where in the
  448. *                initTools procedure the error happened.
  449. *    Outputs:    NONE (program exits)
  450. *    Calls:        closeTools
  451. */
  452.  
  453. /*    The error message is done in this strange way (stuffing hex into the
  454.     middle of a string with Int2Hex) because doing it with a sprintf will
  455.     link in about 20k of code just for this operation.  We don't want to
  456.     do that if we don't have to.  If your application has a need for printf,
  457.     then you may wish to change the way that this works, since you will be
  458.     linking in the 20k for something else anyway.  If you don't do any other
  459.     printf type things, leave this routine the way it is.
  460. */
  461.  
  462. char errStr[] = 
  463.     "Fatal error $xxxx has occurred at xxxx. Press any key to exit:";
  464.  
  465. void errorCheck(where)
  466.     int where;
  467. {
  468.     int theError;
  469.     
  470.     if (_toolErr) {
  471.         theError = _toolErr;                /* store the error number */
  472.         Int2Hex(theError,errStr + 13, 4);    /* Stick error # into a string */
  473.         Int2Hex(where, errStr + 34, 4);        /* Stick loc # into a string */
  474.         
  475.         GrafOff();                            /* Turn off Super Hires */
  476.         WriteCString(errStr);                /* write errStr to text screen */
  477.         SysBeep();                            /* ring the bell */
  478.         ReadChar(noEcho);                    /* wait for keypress & toss it */
  479.  
  480.         closeTools();                        /* ShutDown my Tools */
  481.         exit(1);                            /* quit with APW status = 1 */
  482.     }
  483. }
  484.  
  485.  
  486. /******************************************************************************
  487. *
  488. * initTools:    Load and startup the tools needed. errorCheck is called
  489. *                after each startup to check for errors.
  490. *
  491. *    Inputs:        NONE
  492. *    Outputs:    NONE
  493. *    Calls:        errorCheck
  494. */                    
  495.  
  496. initTools()
  497. {
  498.     TLStartUp();                /* start up Tool Locator   */
  499.     errorCheck(1);                /* Make sure all is OK */
  500.     
  501.     MyMemoryID = MMStartUp();    /* start up Memory Manager & get Memory ID */
  502.     errorCheck(2);                /* Make sure all is OK       */
  503.  
  504.     /* Start up the rest of the tools */
  505.     ToolRecRef = StartUpTools(MyMemoryID,refIsResource,BaseResID+1);
  506.     errorCheck(3);                /* Make sure all is OK */
  507. }
  508.  
  509.  
  510. /******************************************************************************
  511. *
  512. *    main:        This is the main routine. It calls functions to startup
  513. *                the tools, initialize application specific data, run the
  514. *                main eventLoop, close the application, and shutdown the tools.
  515. *                
  516. *    Inputs:        NONE
  517. *    Outputs:    NONE
  518. *    Calls:        initTools, initApp, eventLoop, closeApp, closeTools
  519. */                    
  520.  
  521. main()
  522. {
  523.     initTools();            /* Initialize tools. */
  524.     initApp();                /* Initialize application specific stuff. */
  525.  
  526.     eventLoop();            /* Do application stuff until user wants to */
  527.                             /* do something else!                        */
  528.  
  529.     closeApp();                /* ShutDown application specific things. */
  530.     closeTools();            /* ShutDown the tools. */
  531. }
  532.